/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package engine.skill;

import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import mainpackage.Constant;
import mainpackage.ResManager;
import mainpackage.Tools;

/**
 * @author 技能buff 包括buff 图标 buff提示 buff 持续时间刷新
 */
public class SkillBuff {
    //buff key 

    public String[] iBuffKey;
    //buff类型
    public int[] iType;
    //优先级别
    public int[] iPriority;
    //buff 开始时间
    public long[] iStartTime;
    //buff 持续时间
    public int[] iContinueTime;
    public byte[] iIconType;
    //buff图标
    public Image imgBuffImg;
    //buff持续时间条
    public Image imgContinueBar;
    //索引
    public int iBuffIndex;
    //是否有buff
    public boolean blHasBuff;

    public SkillBuff() {
        //BUFF KEY
        this.iBuffKey = new String[Constant.BUFF_MAX_NUMS];
        //buff类型
        this.iType = new int[Constant.BUFF_MAX_NUMS];
        //优先级别
        this.iPriority = new int[Constant.BUFF_MAX_NUMS];
        this.iIconType = new byte[Constant.BUFF_MAX_NUMS];
        this.iStartTime = new long[Constant.BUFF_MAX_NUMS];
        //buff 持续时间
        this.iContinueTime = new int[Constant.BUFF_MAX_NUMS];
        for (int i = Constant.BUFF_MAX_NUMS - 1; i >= 0; i--) {
            this.iBuffKey[i] = null;
            this.iType[i] = -1;
            this.iPriority[i] = -1;
            this.iContinueTime[i] = -1;
            this.iIconType[i] = -1;
        }
        blHasBuff = false;
        //测试用
    }
    //添加buff

    public void addBuff(int i, String bufKey, int type, int priority, int continueTime, int passTime, byte icontype) {
//        for (int i = 0; i < Constant.BUFF_MAX_NUMS; i++) {
        if (iBuffKey[i] == null) {
            iBuffKey[i] = bufKey;
            iType[i] = type;
            iPriority[i] = priority;
            iContinueTime[i] = continueTime;
            if (passTime == -1) {
                iStartTime[i] = System.currentTimeMillis();
            } else {
                iStartTime[i] = System.currentTimeMillis() - passTime;
            }
            iIconType[i] = icontype;
//                break;
//            }
        }
        blHasBuff = true;
    }

    public void updateBuff() {
        if (!blHasBuff) {
            return;
        }
//        long curentTime = System.currentTimeMillis();
        boolean blbuffclear = true;
        long lcurrentTime;
        for (int i = Constant.BUFF_MAX_NUMS - 1; i >= 0; i--) {
            //更新BUFF影响和冷却时间
            if (iBuffKey[i] != null) {
                blbuffclear = false;
                //本地清楚buff
                lcurrentTime = System.currentTimeMillis();
                if (lcurrentTime - iStartTime[i] >= iContinueTime[i]) {
                    clearBuff(i);
                }
                switch (iType[i]) {
                    case Constant.BUFF_TYPE_CONFUSED:
                        break;
                }
            }
        }
        if (blbuffclear) {
            blHasBuff = false;
        }
    }

    public void drawBuff(Graphics g, int x, int y) {
        if (!blHasBuff) {
            return;
        }
        try {
            if (imgBuffImg == null) {
                imgBuffImg = ResManager.createImage(90000018);
            }
            if (imgContinueBar == null) {
                imgContinueBar = Tools.makeTransparent(Constant.BUFF_ICON_WIDHT, Constant.BUFF_ICON_WIDHT, 100, 0xffffff);
            }
            int dest_y = 0;
            g.setClip(x, y, Constant.BUFF_MAX_NUMS * Constant.BUFF_ICON_WIDHT, Constant.BUFF_ICON_WIDHT);
            long lcurrentTime = System.currentTimeMillis();
            int buffnums = 0;
            for(int i = 0; i < 6; i ++){
                if (iBuffKey[i] != null && iIconType[i] != -1) {
                    dest_y = (int) ((Constant.BUFF_ICON_WIDHT * (lcurrentTime - iStartTime[i]) * 10000 / iContinueTime[i] / 10000));
                    //绘制buff图标
                    g.drawRegion(imgBuffImg, (iIconType[i] - 1) * Constant.BUFF_ICON_WIDHT, 0, Constant.BUFF_ICON_WIDHT, Constant.BUFF_ICON_WIDHT, 0, x + buffnums * Constant.BUFF_ICON_WIDHT, y, 0);
                    if (dest_y >= 0) {
                        g.drawImage(imgContinueBar, x + buffnums * Constant.BUFF_ICON_WIDHT, y - dest_y + Constant.BUFF_ICON_WIDHT, 0);
                    }
                    buffnums += 1;
                }
            }
            g.setClip(0, 0, Constant.SCREEN_WIDTH, Constant.SCREEN_HEIGHT);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void clearBuff(int index) {
        iBuffKey[index] = null;
        iType[index] = -1;
        iPriority[index] = -1;
        iContinueTime[index] = -1;
        iStartTime[index] = -1;
    }
}
